<?php
/**
 * [商网通 System] Copyright swtshop.cn
 * QQ: 1170787711
 * Author: gj.name
 * This is NOT a freeware
*/
defined('IN_UQ') or exit('Access Denied');
class wchat_uqcms extends control
{
    function __construct()
    {
        $this->aid = $_SESSION['admin']['aid'];
        parent::__construct();
    }
    function index()
    {
        $this->display();
    }
    public function auto()
    {
        $query = $this->db->query("select * from " . table('wchat_auto') . " where temp = 0 order by id desc");
        while ($row = $this->db->fetch_array($query)) {
            $data[] = $row;
        }
        $this->assign('data', @$data);
        $this->display();
    }
    public function auto_text()
    {
        if ($_POST) {
            $data['event_key'] = $_POST['name'];
            $data['name'] = $_POST['name'];
            $data['keyword'] = $_POST['keyword'];
            $data['type'] = 'text';
            $data['is_sys'] = '0';
            $data['addtime'] = time();
            if ($this->db->add(table("wchat_auto"), $data)) {
                $msgdata['aid'] = $this->db->insert_id();
                $msgdata['content'] = $_POST['content'];
                $msgdata['addtime'] = time();
                if ($this->db->add(table('wchat_auto_msg'), $msgdata)) {
                    showmsg('修改成功！');
                }
            } else {
                showmsg('修改失败！');
            }
        }
        $this->display();
    }
    public function auto_text_update()
    {
        if ($_POST) {
            $id = $_POST['id'];
            $data['name'] = $_POST['name'];
            $data['event_key'] = $_POST['name'];
            $data['keyword'] = $_POST['keyword'];
            $data['addtime'] = time();
            if ($this->db->update(table("wchat_auto"), $data, ' id =' . $id)) {
                $msgdata['content'] = $_POST['content'];
                $msgdata['addtime'] = time();
                if ($this->db->update(table('wchat_auto_msg'), $msgdata, 'aid =' . $id)) {
                    showmsg('修改成功！');
                }
            } else {
                showmsg('修改失败！');
            }
        }
        $id = $_GET['id'];
        $row = $this->db->get_one("select * from " . table('wchat_auto') . " where id = " . $id);
        $msg_row = $this->db->get_one("select * from " . table('wchat_auto_msg') . " where aid = " . $row['id']);
        $row['content'] = $msg_row['content'];
        $this->assign('row', $row);
        $this->display();
    }
    public function auto_news()
    {
        if ($_POST) {
            $id = $_POST['id'];
            $data['event_key'] = $_POST['event_key'];
            $data['name'] = $_POST['name'];
            $data['keyword'] = $_POST['keyword'];
            $data['type'] = 'news';
            $data['is_sys'] = '0';
            $data['temp'] = '0';
            $data['addtime'] = time();
            if ($this->db->update(table("wchat_auto"), $data, 'id = ' . $id)) {
                showmsg('修改成功！');
            } else {
                showmsg('修改失败！');
            }
        }
        $id = @$_GET['id'];
        if (empty($id)) {
            $row = $this->db->get_one("select * from " . table('wchat_auto') . " where temp = 1 and type = 'news'");
            if (empty($row['id'])) {
                $setsql['type'] = 'news';
                $setsql['temp'] = '1';
                $setsql['addtime'] = time();
                $this->db->add(table('wchat_auto'), $setsql);
                $this->assign('aid', $this->db->insert_id());
            } else {
                $row = $this->db->get_one("select * from " . table('wchat_auto') . " where temp = 1 and type='news'");
                $data = $this->db->get_all("select * from " . table('wchat_auto_msg') . " where aid = " . $row['id'] . " order by id desc");
                $this->assign('data', $data);
                $this->assign('aid', $row['id']);
            }
        } else {
            $row = $this->db->get_one("select * from " . table('wchat_auto') . " where id = " . $id);
            $this->assign('row', $row);
            $data = $this->db->get_all("select * from " . table('wchat_auto_msg') . " where aid = " . $id);
            $this->assign('data', $data);
            $this->assign('aid', $row['id']);
        }
        $this->display();
    }
    public function auto_news_add_save()
    {
        $setsql['aid'] = $_POST['aid'];
        $setsql['title'] = $_POST['title'];
        $setsql['describe'] = $_POST['describe'];
        $setsql['pic_url'] = $_POST['pic_url'];
        $setsql['url'] = $_POST['url'];
        $setsql['addtime'] = time();
        if ($this->db->add(table('wchat_auto_msg'), $setsql)) {
            echo $_POST['aid'];
        }
    }
    public function auto_news_update_box()
    {
        $id = $_GET['id'];
        $row = $this->db->get_one("select * from " . table('wchat_auto_msg') . "  where id = " . $id);
        $this->assign('row', $row);
        $this->display();
    }
    public function auto_news_update_save()
    {
        $id = $_POST['id'];
        $setsql['title'] = $_POST['title'];
        $setsql['describe'] = $_POST['describe'];
        $setsql['pic_url'] = $_POST['pic_url'];
        $setsql['url'] = $_POST['url'];
        $setsql['addtime'] = time();
        $this->db->update(table('wchat_auto_msg'), $setsql, 'id = ' . $id);
        $msg = $this->db->get_one("select * from " . table('wchat_auto_msg') . " id = " . $id);
        echo $msg['aid'];
    }
    public function auto_news_add_del()
    {
        $id = $_POST['aid'];
        if ($this->db->delete(table('wchat_auto_msg'), 'id=' . $id)) {
            echo '1';
        }
    }
    public function auto_del()
    {
        $id = $_GET['id'];
        $this->db->delete(table("wchat_auto"), 'id = ' . $id);
        $this->db->delete(table("wchat_auto_msg"), 'aid = ' . $id);
        showmsg("删除成功！");
    }
    public function menu()
    {
        $query = $this->db->get_all("select * from " . table("wchat_menu") . " order by cid asc");
        if ($query) {
            foreach ($query as $k => $v) {
                if ($v['pcid'] == '0') {
                    $list[] = $v;
                } else {
                    $pcid[$v['pcid']][] = $v;
                }
            }
            foreach ($list as $k => $v) {
                if (isset($pcid[$v['cid']])) {
                    $v['pmenu'] = $pcid[$v['cid']];
                }
                $data[] = $v;
            }
            $this->assign('data', @$data);
        }
        $this->display();
    }
    public function menu_edit()
    {
        $cid = isset($_GET['cid']) ? $_GET['cid'] : false;
        if ($cid) {
            $row = $this->db->get_one("select * from " . table('wchat_menu') . " where cid = " . $cid);
            $this->assign('row', $row);
        }
        $menu = $this->db->get_all("select * from " . table("wchat_menu") . " where pcid = 0 order by cid asc");
        $this->assign('menu', $menu);
        $this->display();
    }
    public function menu_save()
    {
        $setarr['pcid'] = $_POST['pcid'];
        $setarr['name'] = $_POST['name'] ? $_POST['name'] : exit('NAME值不能为空！');
        $event = $_POST['event'];
        if ($event == 'click') {
            $setarr['view_url'] = '';
            $setarr['click_key'] = $_POST['key'] ? $_POST['key'] : exit('KEY不能空！');
        } elseif ($event == 'view') {
            $setarr['view_url'] = $_POST['url'] ? $_POST['url'] : exit('URL不能空！');
            $setarr['click_key'] = '';
        }
        $setarr['event'] = $event;
        $setarr['addtime'] = time();
        $cid = isset($_POST['cid']) ? $_POST['cid'] : false;
        if ($cid) {
            $query = $this->db->update(table("wchat_menu"), $setarr, 'cid = ' . $cid);
        } else {
            $query = $this->db->add(table("wchat_menu"), $setarr);
        }
        if ($query) {
            exit('1');
        } else {
            exit('保存失败');
        }
    }
    public function menu_del()
    {
        $cid = $_GET['cid'] ? $_GET['cid'] : exit('CID不能为空！');
        $query = $this->db->query("delete from " . table('wchat_menu') . " where cid = " . $cid);
        if ($query) {
            showmsg('删除成功！');
        } else {
            showmsg('删除失败！');
        }
    }
    public function menu_create_json()
    {
        $query = $this->db->get_all("select * from " . table("wchat_menu") . " order by cid asc");
        if ($query) {
            foreach ($query as $k => $v) {
                if ($v['pcid'] == '0') {
                    $list[] = $v;
                } else {
                    $wm_data['name'] = urlencode($v['name']);
                    if ($v['event'] == 'click') {
                        $wm_data['type'] = 'click';
                        $wm_data['key'] = $v['click_key'];
                    } else {
                        $wm_data['type'] = 'view';
                        $wm_data['url'] = $v['view_url'];
                    }
                    $pcid[$v['pcid']][] = (object) $wm_data;
                }
            }
            foreach ($list as $k => $v) {
                $wm_data['name'] = urlencode($v['name']);
                if (isset($pcid[$v['cid']])) {
                    $wm_data['sub_button'] = $pcid[$v['cid']];
                    unset($pcid[$v['cid']]);
                } else {
                    if ($v['event'] == 'click') {
                        $wm_data['type'] = 'click';
                        $wm_data['key'] = $v['click_key'];
                    } elseif ($v['event'] == 'view') {
                        $wm_data['type'] = 'view';
                        $wm_data['url'] = $v['view_url'];
                    }
                }
                $data['button'][] = (object) $wm_data;
                unset($wm_data);
            }
        }
        $data = (object) $data;
        $json_data = urldecode(json_encode($data));
        $post_url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" . login_wchat::init()->access_token();
        $result = curl::post($post_url, $json_data);
        $result_state = json_decode($result);
        $code = $result_state->errcode;
        if ($code == '0') {
            exit('1');
        } else {
            exit($code);
        }
    }
    function tpl()
    {
        $url = 'https://api.weixin.qq.com/cgi-bin/template/get_all_private_template?access_token=' . wchat::init()->access_token();
        $result = curl::get($url);
        $query = json_decode($result);
        foreach ($query->template_list as $k => $v) {
            $r['title'] = $v->title;
            $r['template_id'] = $v->template_id;
            $r['deputy_industry'] = $v->deputy_industry;
            $data[] = $r;
        }
        if (isset($data)) {
            $this->assign('data', $data);
        }
        $this->display();
    }
    function tpl_get_industry()
    {
        $url = 'https://api.weixin.qq.com/cgi-bin/template/get_industry?access_token=' . wchat::init()->access_token();
        echo curl::get($url);
    }
    function tpl_set_industry()
    {
        $url = 'https://api.weixin.qq.com/cgi-bin/template/api_set_industry?access_token=' . wchat::init()->access_token();
        $postdata = array('industry_id1' => '1', 'industry_id2' => '4');
        $postdata = (object) $postdata;
        $json_data = urldecode(json_encode($postdata));
        $result = curl::post($url, $json_data);
        $result = json_decode($result);
        if ($result->errmsg) {
            error_json('设置失败' . $result->errmsg);
        } else {
            right_json();
        }
    }
    function tpl_add()
    {
        if ($_POST) {
            $tpl_id = isset($_POST['tpl_id']) ? $_POST['tpl_id'] : error_json('获取模板错误');
            $url = 'https://api.weixin.qq.com/cgi-bin/template/api_add_template?access_token=' . wchat::init()->access_token();
            $postdata = array('template_id_short' => $tpl_id);
            $postdata = (object) $postdata;
            $json_data = urldecode(json_encode($postdata));
            $result = curl::post($url, $json_data);
            $result = json_decode($result);
            if ($result->errmsg) {
                $json_arr['error'] = '0';
                $json_arr['tpl_id'] = $result->template_id;
                echo json_encode($json_arr);
            } else {
                error_json($result->errmsg);
            }
        } else {
            error_json('提交错误');
        }
    }
    function tpl_del()
    {
        $tpl_id = P('id', '模板提交错误');
        $url = 'https://api.weixin.qq.com/cgi-bin/template/del_private_template?access_token=' . wchat::init()->access_token();
        $postdata = array('template_id' => $tpl_id);
        $postdata = (object) $postdata;
        $json_data = urldecode(json_encode($postdata));
        $result = curl::post($url, $json_data);
        $query = json_decode($result);
        if ($query->errcode == '0') {
            right_json();
        } else {
            error_json();
        }
    }
    function tpl_type_arr()
    {
        return array('TM00015', 'TM00303', 'TM00004', 'TM00017');
    }
}